/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-02-28 15:53:33
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-15 17:09:42
 */
// pages/post/shops.js
import {
  userAPI,
  goodsAPI
} from './../../../api/goods'

import {
  returnStatus
} from './../../../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: '已购买',
    result: 'a',
    state: 'selected',
    pageBuyNum: 1,
    requestObj: {},
    buyValues: [],
    buyGoods:[],
    collectGoods:[],
    pageCollectNum:1,
    sbk: {
      disabled: '/pages/images/icon/common_btn_unselected.png',
      selected: '/pages/images/icon/common_btn_selected.png',
      default: '/pages/images/icon/common_btn_default.png'
    }
  },
  switchTag({detail}){
    this.setData({
      active:detail.name
    })
    this.getData()
  },
  onSelectGoods({detail}){
    this.setData({
      buyValues:detail
    })
  },
  onClickLeft(){
    wx.navigateBack();
  },
  onSave(){
    // 将当前信息传递回去
    // 获取到的goods值
    const goods = []
    this.data.buyValues.map(item=>{
      goods.push(this.allGoods.get(item*1))
    })
    this.eventChannel.emit('goods', goods);
    wx.navigateBack();
      

  },
  getData() {
    let url = ''
    if(this.data.active==='已购买'){
      // 购买
      url = 'queryMyBuy'
      this.goods = 'buyGoods'
      this.page = 'pageBuyNum'
    }else{
      url='queryMyCollect'
      this.goods='collectGoods'
      this.page='pageCollectNum'
    }
    return new Promise(async (resolve, reject) => {
      const _ = await goodsAPI[url]({
        pageNum: this.data[this.page],
        pageSize: 10,
      })
      const res = await returnStatus(_)
      if (res) {
        res.records.forEach(item=>{
          this.allGoods.set(item.productId,item)
        })
        this.setData({
          [this.goods]: [...this.data[this.goods], ...res.records],
          [this.page]: ++this.data[this.page]
        })
        resolve(res.records.length)
      } else {
        reject(false)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let app =  getApp();
    app.setTitle('')      
    this.allGoods = new Map()
    this.eventChannel = this.getOpenerEventChannel();
    this.eventChannel.on('params',(buyValues)=>{
      this.setData({
        buyValues
      })
    })
    this.setData({
      requestObj: {
        fetchData: (refresh) => {
          refresh ? this.setData({
            [this.page]: 1,
            [this.goods]: []
          }) : ''
          return this.getData()
        },
      }
    })
    // 获取已经购买的商品及收藏的商品
    // let buy = goodsAPI.queryMyBuy
    // let collect = goodsAPI.queryMyCollect
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})